app.push(function(){
    var Static = function(){
        var modalContainer = $('#helpWindow');
        this.init = function(url){
            url = url || app.currentUrl;
            if(this[url[0]])this[url[0]]();
            else this.showData(url[0]);
        }
        this.showData = function(id){
            app.leftMenu.hide();
            app.topMenu.eraseBreadcrumbList();
            app.topMenu.buildBreadcrumb();
    		$.tmpl( 'static_' + id, {}).prependTo(app.elements.general_content.html(''));
            app.main.unmask();
        }
        this.registered_only = function(){
            this.showData('registered_only');
        }
        this.getContainer = function(){
            return modalContainer;
        }
        var scriptsLoaded = false;
        this.imageUploaderMultiple = function(options, count, newImageFlag){
            var uploaded = 0;
            var element = options.formElement;
            var onStartUploadMultiImages = function(){
                element.find('div[rel=image]').html("");
            }
            var refreshData = function(){
                var container = element.find('div[rel=image]').html("");
                $('#fileupload div.preview img').each(function(){
                    var el = $(this);
                    $.tmpl('multi_image_element', {
                        filename: el.attr('title'),
                        file: el.attr('title'),
                        size: el.attr('data-size'),
                        image_id: el.attr('data-id')
                    }).appendTo(container);
                });
                if(onFullComplete)onFullComplete();
            }
            var onFullComplete = options['onFullComplete'] ? options.onFullComplete : false;
            this.imageUploaderSetup($.extend({
                maxNumberOfFiles: count,
                base_template: 'image_uploader_modal',
                uploadTemplateId: 'jquery_file_uploader-upload',
                downloadTemplateId: 'jquery_file_uploader-download',
                afterCompleted: function(e, data){
                    
                    newImageFlag = true;
                    uploaded++;
                    if(uploaded == data.originalFiles.length){
                        refreshData();
                        setTimeout(function(){
                            app.static.getContainer().modal('hide');
                        },500);
                        
                    }
                    /*if(!data.result[0].error){
                        setTimeout(function(){
                            app.static.getContainer().modal('hide');
                        },500);
                    }*/
                },
                afterDestroyed: function(){
                    refreshData();
                },
                start: function(){
                    uploaded = 0;
                    onStartUploadMultiImages();
                    $('#fileupload .template-upload .progress').parent().show();
                    $('#fileupload .fileupload-buttonbar .cancel').hide();
                }
            }, options));
        }
        this.imageUploaderSingle = function(options, count, newImageFlag){
            var element = options.formElement;
            var onFullComplete = options['onFullComplete'] ? options.onFullComplete : false;
            this.imageUploaderSetup($.extend({
                afterCompleted: function(e, data){
                    element.find('div[rel=image]').html($('<img>').attr({
                        src: data.result[0]['thumbnail_url'] || data.result[0]['url'],
                        class: 'img-rounded'
                    }));
                    element.find('input[name=image_id]').val(data.result[0].id);
                    if(onFullComplete)onFullComplete();
                    if(!data.result[0].error){
                        setTimeout(function(){
                            app.static.getContainer().modal('hide');
                        },500);
                    }
                },
                afterDestroyed: function(){
                    element.find('div[rel=image]').html('');
                    element.find('input[name=image_id]').val(0);
                    image = false;
                    if(onFullComplete)onFullComplete();
                }
            },options));
        }
        this.imageUploaderSetup = function(options){
            options = options || {};
            var container = false;
            var getContainer = function(){
                if(!container) container = $('#fileupload .fileupload-buttonbar');
                return container;
            }
            this.imageUploader($.extend({
                maxNumberOfFiles: 1,
                base_template: 'image_uploader_modal_single',
                uploadTemplateId: 'jquery_file_uploader_single-upload',
                downloadTemplateId: 'jquery_file_uploader_single-download',
                added: function(){
                    getContainer().find('.fileinput-button').hide();
                    getContainer().find('.cancel').show();
                    getContainer().find('.start').show();
                    return true;
                },
                destroyed: function(){
                    getContainer().find('.fileinput-button').show();
                    $('#fileupload').fileupload('enable');
                    getContainer().find('.cancel').hide();
                    getContainer().find('.start').hide();
                    getContainer().find('.delete').hide();
                    if(options['afterDestroyed']!=undefined)options['afterDestroyed']();
                },
                failed: function(){
                    getContainer().find('.fileinput-button').show();
                    $('#fileupload').fileupload('enable');
                    getContainer().find('.cancel').hide();
                    getContainer().find('.start').hide();
                    return true;
                },
                fail: function(){
                    getContainer().find('.fileinput-button').show();
                    $('#fileupload').fileupload('enable');
                    getContainer().find('.cancel').hide();
                    getContainer().find('.start').hide();
                    return true;
                },
                start: function(){
                    $('#fileupload .template-upload .progress').parent().show();
                    $('#fileupload .fileupload-buttonbar .cancel').hide();
                },
                completed: function(e,data){
                    setTimeout(function(){
                        if(data.result[0].error){
                            getContainer().find('.fileinput-button').show();
                        } else {
                            getContainer().find('.delete').show();
                        }
                    },800);
                    if(options['afterCompleted']!=undefined)options['afterCompleted'](e,data);
                }
            },options));
        }
        this.imageUploader = function(options){
            if(!scriptsLoaded){
                /*app.main.loadScript("lib/load-image.min");
                app.main.loadScript("lib/canvas-to-blob.min");
                app.main.loadScript("lib/bootstrap-image-gallery.min");
                app.main.loadScript("lib/jquery.iframe-transport");
                app.main.loadScript("lib/jquery.fileupload");
                app.main.loadScript("lib/jquery.fileupload-fp");
                app.main.loadScript("lib/jquery.fileupload-ui");*/
                scriptsLoaded = true;
            }
            options = options || {};
            app.main.unmask();
            options = $.extend({
                base_template: 'image_uploader_modal',
                url: '/image_upload.php',
                maxFileSize: 5000000,
                acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
                process: [
                    {
                        action: 'load',
                        fileTypes: /^image\/(gif|jpeg|png)$/,
                        maxFileSize: 20000000 // 20MB
                    },
                    {
                        action: 'resize',
                        maxWidth: 1440,
                        maxHeight: 900
                    },
                    {
                        action: 'save'
                    }
                ]
            },options);
            $.tmpl(options.base_template, {}).appendTo(modalContainer.html(''));
            $('#fileupload').fileupload(options);
            if(options['list'] && $.isArray(options['list'])){
                $('#fileupload').fileupload('renderPresentFiles');
            }
            modalContainer.modal({
                backdrop: 'static'
            });
            //container.modal('show');
        }
        var showDialog = function(options){
            /*$.tmpl('modal_message', options).appendTo(modalContainer.html(''));
            modalContainer.modal('show');
            if(options['delay']){
                setTimeout(function(){
                    modalContainer.modal('hide');
                },options['delay']);
            }*/
            var options = options;
            $('#top_message_element').html('<h4>'+options.title+'</h4>'+options.description)
                .attr('class',"span12 alert alert-"+options.type)
                .slideDown(function(){
                    var el = $(this);
                    setTimeout(function(){
                        el.slideUp();
                    },options.delay);
                });
        }
        this.success = function(options){
            showDialog($.extend({
                type: 'success',
                title: 'Операция прошла успешно',
                description: 'У вас всё получилось',
                delay: 3000
            },options))
        }
        this.error = function(options){
            showDialog($.extend({
                type: 'error',
                title: 'Операция провалилась',
                description: 'Что то пошло не так',
                delay: 6000
            },options));
        }
        this.info = function(options){
            showDialog($.extend({
                type: 'info',
                title: 'Заголовок',
                description: 'Текст',
                delay: 0
            },options));
        },
        this.prompt = function(options){
            var options = $.extend({
                type: 'info',
                title: 'Заголовок',
                description: 'Текст',
                buttons:[{
                    name: "Ok",
                    className: ' btn-success',
                    action: function(){
                        modalContainer.modal('hide');
                    }
                }]
            },options);
            $.tmpl('modal_prompt', options).appendTo(modalContainer.html(''));
            modalContainer.modal('show');
            var tmp=0;
            modalContainer.find('[type=button]').each(function(){
                $(this).click(options.buttons[tmp].action);
                tmp++;
            });
        },
        this.image = function(options){
            options = $.extend({
                type: 'info',
                title: 'Картинка',
                img: '',
                delay: 0
            },options);
            $.tmpl('modal_image', options).appendTo(modalContainer.html(''));
            modalContainer.modal('show');
            if(options['delay']){
                setTimeout(function(){
                    modalContainer.modal('hide');
                },options['delay']);
            }
        },
        this.imageGallery = function(element,list,first,options){
            var el = [];
            var this_ = this;
            $(list).each(function(){
                el[this.image_id] = $('<img>').attr('src','/data/100_100/' + this.filename)
                    .attr('class',(this.image_id == first ? 'big_image' : 'small_image'))
                    .attr('data-href','/data/origin/' + this.filename)
                    .click(function(){
                        var el = $(this);
                        if(el.hasClass('small_image_container')) el = el.find('img:first');
                        if(el.hasClass('small_image')){
                            var big = el.parents('.image_gallery_box').find('.big_image:first');
                            big.css({height: 'auto',width:'auto'});
                            var tmp = big.attr('data-href');
                            big.attr('data-href',el.attr('src'));
                            el.attr('src',tmp);
                            tmp = big.attr('src');
                            big.attr('src',el.attr('data-href'));
                            el.attr('data-href',tmp);
                        }
                    });
            });
            if(el[first]){
                var tmp = el[first].attr('src');
                el[first].attr('src', el[first].attr('data-href'));
                el[first].attr('data-href', tmp)
                    .addClass('img-rounded')
                    .click(function(){
                        this_.image({
                            img: $(this).attr('src'),
                        })
                    })
                    .load(function(){
                        var el = $(this);
                        var parent = el.parents('.image_gallery_box');
                        if(el.width() > el.height()){
                            parent.addClass('hor');
                            if(el.width()<parent.width()){
                                el.css('width','100%');
                            }
                        } else {
                            parent.removeClass('hor');
                            if(el.height()<parent.height()){
                                el.css('height','100%');
                            }
                        }
                    });
                element.append($('<div>').attr('class','big_image_container')
                    .append(el[first])
                )
            }
            for(var k in el){
                if(isNaN(k))break;
                if(k == first) continue;
                element.append(
                    $('<div>').attr('class','small_image_container img-rounded')
                        .append(el[k])
                );
            }
            element.append($('<div>').attr('class','clearfix'));
        }
    }
    app['static'] = new Static();
});